package com.nl.controller.web.controller;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.nl.controller.common.model.Response;
import com.nl.controller.common.service.CommonService;
import com.nl.controller.common.util.ResponseUtil;
import com.nl.controller.web.bean.ICT;
import com.nl.controller.web.bean.ICTTable;
import com.nl.controller.web.service.IctService;
import com.nl.controller.web.util.ExcelUtil;

import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;


@RestController
@Slf4j
@RequestMapping("ict")
public class IctController {
	
	@Autowired
	CommonService commonService;
    @Autowired
    private IctService ictService;
    private HashMap< String , String > map = new HashMap<String, String>();
    private HashMap<String, ICTTable> mapState = new HashMap<String, ICTTable>();
    
    @ApiOperation(value = "ict", notes = "ict周报")
    @PostMapping("find")
    public Response findIct() {
    	int result;
    	//根据最后时间作判断
    	result = ictService.findEndDate(map);
        ICTTable ictTable = new ICTTable();
        try {
        	if(result==1){
            ictTable = ictService.getDataList();
            mapState.put("ictTable",ictTable);
        	}else{
        	ictTable=(ICTTable) mapState.get("ictTable");
        	}
        } catch (Exception e) {
            e.printStackTrace();
        }
        log.info("ict周报");
        return ResponseUtil.sucess(ictTable);
    }

    @ApiOperation(value = "ict", notes = "ict周报导出")
    @GetMapping("download")
    public void downLoad(HttpServletRequest request,HttpServletResponse response) {
        ICTTable ictTable;
        //ictTable = ictService.getDataList();
        ictTable=(ICTTable) mapState.get("ictTable");
        if (ictTable.getDataList().size() > 0) {
            try {
                String title[] = getString(ictTable.getTitleList().toArray());
                List<String[]> dataList = new ArrayList<String[]>();
                List<ICT> icts = ictTable.getDataList();
                ICT tempIct = icts.get(0);
                int seq = 1;
                //序号添加
                for (int i = 0; i < icts.size(); i++) {
                    ICT ict = icts.get(i);
                    if (!tempIct.getCityName().equals(ict.getCityName())) {
                        seq++;
                        ict.setSeq(String.valueOf(seq));
                        tempIct = ict;
                    } else {
                        ict.setSeq(String.valueOf(seq));
                    }
                }
                for (ICT a : icts) {
                    List<String> list = new ArrayList<>();
                    list.add(a.getSeq());
                    list.add(a.getCityName());
                    list.add(a.getCountyName());
                    list.add(a.getMgrName());
                    list.add(a.getCreateTime());
                    list.add(a.getWeekData());
                    for (int i = 0; i < a.getMonthData().size(); i++) {
                        list.add(a.getMonthData().get(i));
                    }
                    list.add(a.getNowMonthData());
                    list.add(a.getNowYearData());
                    list.add(a.getNowCityMonthData());
                    list.add(a.getNowCityYearData());
                    list.add(a.getCityName2());
                    dataList.add(getString(list.toArray()));
                }
                ExcelUtil.exportExelMerge("欢迎名片业务已经开通地区汇总.xls", title, dataList, false,request, response, new Integer[]{0, 1,title.length - 1}, new Integer[]{0, 1, title.length - 3,title.length - 2,title.length - 1}, null, null);
               
                commonService.createSmsOperLog("7", "0","ICT周报数据导出", "0", "26");
                
            } catch (Exception e) {
                e.printStackTrace();
                commonService.createSmsOperLog("7", "0","ICT周报数据导出", "1", "26");
            }
            log.info("ict周报");
        } else {
            log.info("数据为空");
        }
    }


    private String[] getString(Object[] obj) {
        String[] strs = new String[obj.length];
        for (int i = 0; i < obj.length; i++) {
            strs[i] = obj[i].toString();
        }
        return strs;
    }
}
